class Solution:
    def longestConsecutive(self, nums):
        longest_streak = 0
        num_set = set(nums)  # 去重

        for num in num_set:  # 遍历
            if num - 1 not in num_set:  # 减少重复工作，只找头元素
                current_streak = 1

                while num + 1 in num_set:
                    num += 1
                    current_streak += 1

                longest_streak = max(longest_streak, current_streak)

        return longest_streak
